<template>
  <!-- 日期选择器 -->
  <el-date-picker
    v-model="dataValue"
    size="small"
    type="datetimerange"
    align="right"
    :start-placeholder="$t('manage.开始日期')"
    :end-placeholder="$t('manage.结束日期')"
    format="yyyy-MM-dd HH:mm:ss"
    value-format="yyyy-MM-dd HH:mm:ss"
    :default-time="['00:00:00', '23:59:59']"
    :picker-options="pickerOptions"
  />
</template>

<script>
export default {
  name: 'TimePickerComp',
  data() {
    return {
      pickerOptions: {
        disabledDate(time) {
          // return time.getTime() > Date.now() // 禁选超过当前时间的日期
          const currentDate = new Date()
          const selectedDate = new Date(time.getFullYear(), time.getMonth(), time.getDate())
          return selectedDate > currentDate // 禁用超过当前日期的时间点（仅比较年月日）
        },
        shortcuts: [
          {
            text: '今天',
            onClick(picker) {
              const today = new Date()
                .toISOString()
                .split('T')[0]
              picker.$emit('pick', [today + ' 00:00:00', today + ' 23:59:59'])
            }
          },
          {
            text: '昨天',
            onClick(picker) {
              const yesterday = new Date()
              yesterday.setTime(yesterday.getTime() - 3600 * 1000 * 24)
              const yesterdayStr = yesterday
                .toISOString()
                .split('T')[0]
              const today = new Date()
                .toISOString()
                .split('T')[0]
              picker.$emit('pick', [
                yesterdayStr + ' 00:00:00',
                today + ' 23:59:59'
              ])
            }
          },
          {
            text: '一周内',
            onClick(picker) {
              const week = new Date()
              week.setTime(week.getTime() - 3600 * 1000 * 24 * 7)
              const weekStr = week
                .toISOString()
                .split('T')[0]
              const today = new Date()
                .toISOString()
                .split('T')[0]
              picker.$emit('pick', [weekStr + ' 00:00:00', today + ' 23:59:59'])
            }
          },
          {
            text: '一月内',
            onClick(picker) {
              const month = new Date()
              month.setMonth(month.getMonth() - 1)
              const monthStr = month
                .toISOString()
                .split('T')[0]
              const today = new Date()
                .toISOString()
                .split('T')[0]
              picker.$emit('pick', [
                monthStr + ' 00:00:00',
                today + ' 23:59:59'
              ])
            }
          }
        ]
      },
      dataValue: [] // 日期选择值
    }
  },
  methods: {
    // 日期范围选择器初始化默认值
    initTimeData() {
      const today = new Date()
        .toISOString()
        .split('T')[0]
      this.dataValue = [today + ' 00:00:00', today + ' 23:59:59']
    }
  }
}
</script>

<style>
</style>
